// pages/agriculturalclass/agriculturalclass.js
const db = wx.cloud.database();
const app = getApp();
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: [{
      id: 0,
      url: "https://636c-cloud1-1ga8zsp16c57584a-1308614351.tcb.qcloud.la/class/banner1.svg?sign=5d12f1d29f251e4be0b1a30388a753f9&t=1655959132"
    }],
    // canLike: true,
    recommendCourse: []
  },
  SwiperChange(es) {
    this.setData({
      centerItem: e.detail.current,
    })
  },
  navToSearch() {
    console.log("搜索");
    wx.navigateTo({
      url: '/pages/searchCourse/searchCourse',
    })
  },
  gethome() {
    wx.navigateBack({
      url: "/pages/home/home"
    })
  },
  addSee(e) {
    let that = this;
    console.log(e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index;
    let course_id = that.data.recommendCourse[index]._id;
    db.collection('userCollect').where({
      _openid: app.personInfo._openid,
      course_id: course_id
    }).get({
      success: res => {
        if (res.data.length == 0) {
          //没点过，现在点
          db.collection('userCollect').add({
            data: {
              type: "访问量",
              _openid: app.personInfo._openid,
              course_id: course_id
            },
            success: res => {
              console.log("成功记录访问者");
            }
          })
          db.collection('course').doc(course_id).update({
            data: {
              see: _.inc(1)
            },
            success: res => {
              that.setData({
                ['recommendCourse[' + index + '].see']: that.data.recommendCourse[index].see + 1,
              });
              console.log('当前课程的点击量：' + that.data.recommendCourse[index].see);
            }
          })
        } else {
          console.log("用户已经访问过这个课程了");
        }
      },
      complete() {
        that.transformationcard(e.currentTarget.dataset.correspondinginfo);
      }
    })
  },


  transformationcard(correspondinginfo) {
    getApp().navTmpInfo = correspondinginfo;

    wx.navigateTo({
      url: "/pages/videocard/videocard"
    })
  },


  transformationnewclass() {
    console.log("newclass")
    wx.navigateTo({
      url: "/pages/newclass/newclass"
    })
  },
  transformationallclass() {
    console.log("allclass")
    wx.navigateTo({
      url: "/pages/allclass/allclass"
    })
  },
  transformationrankinglist() {
    console.log("rankinglist")
    wx.navigateTo({
      url: "/pages/rankinglist/rankinglist"
    })
  },
  transformationcollectCourse() {
    console.log("collectCourse")
    wx.navigateTo({
      url: "/pages/collectCourse/collectCourse"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let that = this;
    db.collection('course').where({
        recommend: 1
      })
      .get({
        success: function (res) {
          console.log(res.data);
          that.setData({
            recommendCourse: res.data
          });
        }
      })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {



  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})